<?php

declare(strict_types=1);

namespace App\Service\Business;

use App\Service\Access\CustomModuleDataAccess;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 自定义模块数据-业务类
 */
class CustomModuleDataBusiness extends BaseBusiness
{
        #[Inject]
        protected CustomModuleDataAccess $CustomModuleDataAccess;

        /**
         * 创建
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function create(): array
        {
                $arrInsert = [
                        'customModuleDataId' => snowflakeId(),
                        'customModuleId' => $this->request->input('customModuleId'),
                        'status' => $this->request->input('status'),
                        'sort' => $this->request->input('sort', DEFAULT_SORT),
                        'createTime' => $this->request->getAttribute('timestamp'),
                ];
                $bool = $this->CustomModuleDataAccess->insert($arrInsert);
                if (!$bool) return businessError();
                $this->createOperateLog([], $arrInsert);
                return businessSuccess(['customModuleDataId' => $arrInsert['customModuleDataId']]);
        }

        /**
         * 更新
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function update(): array
        {
                $arrData = $this->CustomModuleDataAccess->find($this->request->input('customModuleDataId'),
                        ['customModuleId', 'status', 'sort', 'updateTime']);
                $arrUpdate = [
                        'customModuleDataId' => $this->request->input('customModuleDataId'),
                        'customModuleId' => $this->request->input('customModuleId'),
                        'status' => $this->request->input('status'),
                        'sort' => $this->request->input('sort', DEFAULT_SORT),
                        'updateTime' => $this->request->getAttribute('timestamp'),
                ];
                $intEffectRow = $this->CustomModuleDataAccess->updateById($arrUpdate);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData, $arrUpdate);
                return businessSuccess();
        }

        /**
         * 分页
         * @param array $inject
         * @return array
         */
        public function paginate(array $inject = []): array
        {
                $arrQueryResult = $this->CustomModuleDataAccess->paginate($inject);
                $arrResult = [
                        'page' => $arrQueryResult['current_page'],
                        'limit' => $arrQueryResult['per_page'],
                        'total' => $arrQueryResult['total'],
                        'list' => $arrQueryResult['data'],
                ];
                return businessSuccess($arrResult);
        }

        /**
         * 删除
         * @param array $inject
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function delete(array $inject = []): array
        {
                $arrData = $this->CustomModuleDataAccess->find($this->request->input('customModuleDataId'));
                $intEffectRow = $this->CustomModuleDataAccess->delete($inject ?: [
                        'wheres' => ['customModuleDataId' => $this->request->input('customModuleDataId')]
                ]);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData);
                return businessSuccess();
        }

        /**
         * 关联性删除
         * @param mixed $customModuleId
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function relevanceDelete(mixed $customModuleId): array
        {
                $arrData = $this->CustomModuleDataAccess->forPage([
                        'wheres' => ['customModuleId' => $customModuleId], 'page' => 1, 'limit' => 10000,
                        'fields' => ['customModuleDataId', 'customModuleId', 'status', 'sort', 'createTime', 'updateTime'],
                ]);
                $intEffectRow = $this->CustomModuleDataAccess->delete(['wheres' => ['customModuleId' => $customModuleId]]);
                if (!$intEffectRow) return businessError();
                $this->createOperateLog($arrData);
                return businessSuccess();
        }
}